// CommentController.java
package com.health.information.controller;

import java.util.List;

import com.health.information.domain.ContentInformation;
import com.health.information.service.IContentInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.health.common.core.controller.BaseController;
import com.health.common.core.domain.AjaxResult;
import com.health.information.domain.CommentVO;
import com.health.information.service.ICommentService;

/**
 * 评论Controller
 */
@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController {

    @Autowired
    private ICommentService commentService;
    @Autowired
    private IContentInformationService contentInformationService;

    /**
     * 查询评论列表
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam Long postId) {
        List<CommentVO> list = commentService.selectCommentVOList(postId);
        return success(list);
    }

    /**
     * 点赞文章
     */
    @PostMapping("/{articleId}/like")
    public AjaxResult like(@PathVariable Long articleId) {
        ContentInformation article = contentInformationService.selectContentInformationByArticleId(articleId);
        if (article != null) {
            article.setLikeNum((article.getLikeNum() == null ? 0 : article.getLikeNum()) + 1);
            contentInformationService.updateContentInformation(article);
            return success();
        }
        return error("文章不存在");
    }

    /**
     * 取消点赞
     */
    @PostMapping("/{articleId}/cancelLike")
    public AjaxResult cancelLike(@PathVariable Long articleId) {
        ContentInformation article = contentInformationService.selectContentInformationByArticleId(articleId);
        if (article != null) {
            article.setLikeNum(Math.max(0, (article.getLikeNum() == null ? 0 : article.getLikeNum()) - 1));
            contentInformationService.updateContentInformation(article);
            return success();
        }
        return error("文章不存在");
    }
}